package com.hebxy.spring.dao;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.hebxy.spring.beans.User;
@Repository
public class UserDaoImpl {
	private JdbcTemplate t;	
	public void setDataSource(DataSource dataSource){
		t = new JdbcTemplate(dataSource);
	}
	public int addUser(User user){
		String sql = "INSERT INTO hebxy.user (NAME,PASSWORD) VALUES(?,?)";
		return t.update(sql,user.getName(),user.getPassWord());
		}
	public int updateUser(User user){
		String sql = " UPDATE user SET NAME = ?, PASSWORD = ? WHERE id = ?";
		return t.update(sql,user.getName(),user.getPassWord());
	}

	public int deleteUser(User user){
		String sql = "delete * from user where id = ?";
		return t.update(sql,user.getName(),user.getPassWord());
	}

	public User getUserById(int id){
		String sql = "select * from user where id = ?";
		return t.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class),id);
	}

	public List<User> getUserList(int id){
		String sql = "select * from user";
		return t.query(sql, new BeanPropertyRowMapper<User>(User.class));
	}

	public Map<String,Object> getUserMap(int id){
		String sql = "select * from user where id="+id;
		return t.queryForMap(sql);
	}

	public int getUserCount(){
		return t.queryForObject("select count(*) from user", Integer.class);
	}
}
